<?php
  
  define('IN_ECS', true);
  
  define('ROOT_PATH', str_replace('news.php', '', str_replace('\\', '/', __FILE__)));
  require_once(ROOT_PATH.'ty/NewsService.php');
  require_once(ROOT_PATH.'ty/TyUtils.php');
  require_once(ROOT_PATH.'plugins/JSON.php');
  
  $newsService = new NewsService();
  $smarty = TyUtils::getSmarty();
  
  $action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'news_tl_index';
  
  if($action == 'news_tl_index'){
      
      $newsList = $newsService->getNewsShowToIndexGroupCat();
      $smarty->assign('news_list', $newsList);
      $content = $smarty->fetch('index/news_title_list.inc');
      exit($content);
  }
  elseif($action == 'news_cates_list'){
      //$cateId = addslashes(trim($_REQUEST['catid']));
      $cateId = $_REQUEST['catid'];
      $cate = $newsService->getCategoryByCateId($cateId);
      $cateList = $newsService->getCategoryByParentId($cate['parent_id']);
      
      $newsList = $newsService->getNewsByCat($cate['parent_id']);

      $smarty->assign('newslist', $newsList);
      $smarty->assign('current_cat', $cate);
      $smarty->assign('cate_list', $cateList);
      $newsList = $newsService->getNewsByCat($cateId);
      $smarty->assign('news_list', $newsList);
      
      exit($smarty->fetch('newslist/news_cate.inc'));
  }
  elseif($action == 'child_newscates'){
      $cateId = $_REQUEST['catid'];

      $currentCat = $newsService->getCategoryByCateId($cateId);
      $newsOfCat = $newsService->getNewsByCat($cateId);
      $cateList = $newsService->getCategoryByParentId($cateId);

      $smarty->assign('current_cat', $currentCat);
      $smarty->assign('news_list', $newsOfCat);
      $smarty->assign('cate_list', $cateList);
      $smarty->display('newslist/child_newscate.inc');
  }
  elseif($action == 'ajax_news_content'){
      $newsId = $_REQUEST['newsid'];
      $newsInfo = $newsService->getNewsInfoByNewsId($newsId);

      $json = new Services_JSON();

      exit($json->encode($newsInfo));

  }
  elseif($action == 'ajax_cate_list'){
      $cateId = $_REQUEST['catid'];
      $currentCate = $newsService->getCategoryByCateId($cateId);
      $newsList = $newsService->getNewsByCat($cateId);
      $json = new Services_JSON();
      $result = array('list'=>$newsList, 'current_cate'=>$currentCate);
      exit($json->encode($result));
  }
  elseif($action == 'news_content'){
      $newsId = $_REQUEST['newsid'];
      
      $newsInfo = $newsService->getNewsInfoByNewsId($newsId);
      if(!isset($newsInfo) || empty($newsInfo)){
          $newsInfo['news_title'] = '此新闻内容已不存在!';
          $smarty->assign('news_info', $newsInfo);
          exit($smarty->fetch('newslist/news_content.inc'));
      }
      
      $smarty->assign('news_info', $newsInfo);
      
      $cateId = $newsInfo['category_id'];
      $cate = $newsService->getCategoryByCateId($cateId);
      $cateList = $newsService->getCategoryByParentId($cate['parent_id']);
      
      $smarty->assign('current_cat', $cate);
      $smarty->assign('cate_list', $cateList);
      $newsList = $newsService->getNewsByCat($cateId);
      $smarty->assign('news_list', $newsList);
      
      exit($smarty->fetch('newslist/news_content.inc'));
  }
  elseif ($action == 'menuindex'){
  	$menuList = $newsService->getIndexMenu(8);
  	
  	require_once(ROOT_PATH.'plugins/JSON.php');
  	$json = new Services_JSON();
  	$result = array('list'=>$menuList);
	exit($json->encode($result));  	
  }
  
?>
